<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<input type="text" id="txt">
</body>
<script type="text/javascript">
	var txt = document.getElementById('txt');
	txt.onkeydown = function(e){
		var e = e || event;
		console.log(e.keyCode);
		if ((e.keyCode<48 || e.keyCode>57)&&(e.keyCode != 8)) {
			event.preventDefault();
		}
	}
</script>
</html>